/**
 * @project name:crm
 */
package com.yunbo.crm.globalExceptionHandler;

import com.yunbo.crm.base.ResultInfo;
import com.yunbo.crm.exceptions.ParamsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2021/1/20 16:45
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@ControllerAdvice
public class GlobalExceptionHandlerJSON {

    private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandlerJSON.class);

    @ExceptionHandler({ParamsException.class})
    @ResponseBody
    public ResultInfo jsonExceptionHandler(HttpServletRequest request, ParamsException e) {
        ResultInfo resultInfo = new ResultInfo();
        //打印异常URL和异常内容
        logger.error("Error_URL：" + request.getRequestURI(), e);
        //统一返回101码
        resultInfo.setCode(e.getCode());
        //设置返回的错误信息
        resultInfo.setMsg(e.getMessage());
        return resultInfo;
    }
}